import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:taonile/config/config.dart';

import '../entity/user_info_entity.dart';
import '../http/api.dart';
import '../provider/userinfo_provider.dart';
import '../routers/routers.dart';
import '../utils/user_utils.dart';
import '../views/dialog_manager.dart';

class MineSetting extends StatefulWidget {
  const MineSetting({Key? key}) : super(key: key);

  @override
  State<MineSetting> createState() => _MineSettingState();
}

class _MineSettingState extends State<MineSetting> {
  ///
  /// 返回普通的列表item
  ///
  Widget getListItem(IconData icon, String title, String subTitle, Function() onTap) {
    return ListTile(
      onTap: onTap,
      leading: Icon(icon, size: 43.w),
      title: Text(title, style: TextStyle(fontSize: 25.w)),
      horizontalTitleGap: 0,
      dense: true,
      iconColor: Config.colorText1,
      textColor: Config.colorText1,
      trailing: SizedBox(
        width: 300.w,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Text(subTitle, style: TextStyle(fontSize: 23.w, color: Config.colorText3)),
            Padding(padding: EdgeInsets.all(10.w)),
            Icon(Icons.arrow_forward_ios, size: 20.w),
          ],
        ),
      ),
      contentPadding: EdgeInsets.symmetric(vertical: 9.w, horizontal: 0),
    );
  }

  Widget getLoginOutView() {
    UserInfoEntity infoEntity = Provider.of<UserInfoProvider>(context).userInfo;
    if (infoEntity.code == 0) {
      return const Padding(padding: EdgeInsets.all(0));
    } else {
      return Container(
        width: double.infinity,
        height: 90.w,
        padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0.w),
        margin: EdgeInsets.symmetric(vertical: 10.w, horizontal: 25.w),
        child: ElevatedButton(
          onPressed: () {
            DialogManager.showDefaultDialog(context, "确认退出登录吗", () async {
              await Api.loginOut();
              UserUtils.loginOut(context);
              Routes.router.pop(context);
            });
          },
          style: ElevatedButton.styleFrom(
            primary: Config.colorMain,
            onPrimary: Config.colorBg,
            shadowColor: Config.colorBg,
            elevation: 0,
            shape: const StadiumBorder(),
          ),
          child: const Text(
            "退出登录",
            style: TextStyle(color: Config.colorWhite),
          ),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Config.colorBg2,
        appBar: AppBar(
          title: const Text('设置'),
          centerTitle: true,
        ),
        body: Container(
          height: double.infinity,
          width: double.infinity,
          child: Column(
            children: [
              Container(
                //中间列表区
                width: double.infinity,
                height: 480.w,
                padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0.w),
                margin: EdgeInsets.symmetric(vertical: 30.w, horizontal: 30.w),
                decoration: BoxDecoration(
                  color: (Config.colorWhite),
                  borderRadius: BorderRadius.circular(12.w),
                ),
                child: ListView(
                  physics: const NeverScrollableScrollPhysics(),
                  padding: EdgeInsets.symmetric(vertical: 6.w, horizontal: 30.w),
                  children: [
                    getListItem(Icons.perm_contact_cal_outlined, "个人设置", "", () async {
                      if (await UserUtils.isLogin() == false) {
                        return;
                      }
                      Routes.navigateTo(context, Routes.mineInfo);
                    }),
                    Divider(
                      height: 2.w,
                      color: Config.colorDivider,
                    ),
                    getListItem(Icons.safety_check_outlined, "账户安全", "", () async {
                      if (await UserUtils.isLogin() == false) {
                        return;
                      }
                      Routes.navigateTo(context, Routes.mineAccountSafety);
                    }),
                    Divider(
                      height: 2.w,
                      color: Config.colorDivider,
                    ),
                    getListItem(Icons.account_box_outlined, "联系我们", "", () {
                      Routes.navigateTo(context, Routes.mineAbout);
                    }),
                    Divider(
                      height: 2.w,
                      color: Config.colorDivider,
                    ),
                    getListItem(Icons.cached, "清理缓存", "0KB", () {
                      DialogManager.showDefaultDialog(context, "确认清理缓存吗", () {});
                    }),
                  ],
                ),
              ),
              getLoginOutView(),
            ],
          ),
        ));
  }
}
